import Vue from 'vue'
import VMain from './ImagePreview.vue'
// 返回一个 扩展实例构造器
const Constructor = Vue.extend(VMain)
// const defaultOptions = {}
const MyPlugin = {
  install: function(Vue, _globalOptions = {}) {
    Vue.prototype.$imagePreview = function(images, index = 0) {
      // console.log(images)
      // const options = Object.assign(defaultOptions, _globalOptions)
      images.forEach(e => {
        if (e.w === undefined || e.h === undefined) {
          const img = new Image()
          img.src = e.src
          // img.onload = function() {
          e.w = img.width
          e.h = img.height
          // }
        }
        return e
      })
      const dom = new Constructor({
        el: document.createElement('div'),
        data: {
          images: images,
          index: parseInt(index, 10)
        }
      })
      // 把 实例化的 ImagePreview.vue 添加到 body 里
      document.body.appendChild(dom.$el)
      // document.getElementById('app-wrap').appendChild(dom.$el)
    }
  }
}
if (typeof window !== 'undefined' && window.Vue) {
  window.Vue.use(MyPlugin.install)
}
export default MyPlugin
